﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Parking.Models;

namespace Parking.Controllers
{
    [Authorize]
    public class CompanyInfoController : Controller
    {
        private ICompanyInfoRepository _repository;
        private SelectList typeList = new SelectList(new[] { "Meeting", "Requirements", "Development", "Testing", "Documentation" });

        public CompanyInfoController() : this(new CompanyInfoRepository())
        {

        }

        public CompanyInfoController( ICompanyInfoRepository repository)
        {
            _repository = repository;
        }
        //
        // GET: /ComanyInfo/

        public ActionResult Index()
        {
            return View(_repository.GetCompanies());
        }

        //
        // GET: /ComanyInfo/Details/5

        public ActionResult Details(string name)
        {
            CompanyInfo company = _repository.GetCompanyByName(name);
            if (company == null)
                return RedirectToAction("Index");

            return View(company);
        }

        //
        // GET: /ComanyInfo/Create

        public ActionResult Create()
        {
            ViewBag.TypeList = typeList;            
            return View();
        } 

        //
        // POST: /ComanyInfo/Create

        [HttpPost]
        public ActionResult Create(CompanyInfo companyInfo)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    _repository.InsertCompany(companyInfo);
                    return RedirectToAction("Index");
                }
                catch (Exception ex)
                {
                    //error msg for failed insert in XML file
                    ModelState.AddModelError("", "Error creando registro en el archivo .xml" + ex.Message);
                }
            }

            return View(companyInfo);
        }
        
        //
        // GET: /ComanyInfo/Edit/5
 
        public ActionResult Edit(string name)
        {
            CompanyInfo companyInfo = _repository.GetCompanyByName(name);
            if (companyInfo == null)
                return RedirectToAction("Index");

            ViewBag.TypeList = typeList;
            return View(companyInfo);
        }

        //
        // POST: /ComanyInfo/Edit/5

        [HttpPost]
        public ActionResult Edit(CompanyInfo companyInfo)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    _repository.EditCompany(companyInfo);
                    return RedirectToAction("Index");
                }
                catch (Exception ex)
                {
                    //error msg for failed edit in XML file
                    ModelState.AddModelError("", "Error editando registro. " + ex.Message);
                }
            }

            return View(companyInfo);
        }

        //
        // GET: /ComanyInfo/Delete/5
 
        public ActionResult Delete(string name)
        {
            CompanyInfo companyInfo = _repository.GetCompanyByName(name);
            if (companyInfo == null)
                return RedirectToAction("Index");
            return View(companyInfo);
        }

        //
        // POST: /ComanyInfo/Delete/5

        [HttpPost]
        public ActionResult Delete(string name, FormCollection collection)
        {
            try
            {
                _repository.DeleteCompany(name);
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                //error msg for failed delete in XML file
                ViewBag.ErrorMsg = "Error borrando registro en el archivo .xml. " + ex.Message;
                return View(_repository.GetCompanyByName(name));
            }
        }
    }
}
